import Vue from 'vue'
import VueRouter from 'vue-router'

// 不需要懒加载的
import Login from '@/views/login'
import Layout from '@/views/layout'
// import Search from '@/views/search'
// import SearchResultList from '@/views/search/resultList'
import Home from '@/views/layout/home'
// import User from '@/views/layout/user'
// import UserEdit from '@/views/userEdit'
// import ArticleDetail from '@/views/article/id'
// import Chat from '@/views/chat'

// 需要懒加载的 写成函数的格式 webpack打包的时候，看到这个表示就不会加载这个js文件
// webpackChunkName 用来指明 打包后 生成的文件名
// 提高首次的加载速度，对于看不到的页面通过懒加载来限制加载
const Search = () => import(/* webpackChunkName: Search */'@/views/search')
const SearchResultList = () => import(/* webpackChunkName: SearchResultList */'@/views/search/resultList')
const UserEdit = () => import(/* webpackChunkName: UserEdit */'@/views/userEdit')
const User = () => import(/* webpackChunkName: User */'@/views/layout/user')
const ArticleDetail = () => import(/* webpackChunkName: ArticleDetail */'@/views/article/id')
const Chat = () => import(/* webpackChunkName: Chat */'@/views/chat')

Vue.use(VueRouter)

const routes = [
  // 根路径重定向
  {
    path: '/',
    redirect: '/layout'
  },
  // 登录页
  {
    path: '/login',
    component: Login
  },
  {
    path: '/chat',
    component: Chat
  },
  {
    path: '/user/userEdit',
    component: UserEdit
  },
  // 跳转到搜索页面
  {
    path: '/search',
    component: Search
  },
  // 文章详情页
  {
    path: '/article',
    component: ArticleDetail
  },
  // 搜索结果页
  {
    path: '/search/:keyword', // 动态路由
    // name: 'resultList',
    component: SearchResultList
  },
  // 首页
  {
    path: '/layout',
    component: Layout,
    // 默认显示二级页面为home首页
    redirect: '/layout/home',
    children: [
      {
        path: 'home', // 路径写成空字符串 也可以完成二级页面的跳转
        component: Home,
        meta: { // 路由元信息 给路由对象添加额外的信息
          scrollY: 0 // 记录当前滚动条的位置
        }
      },
      {
        path: 'user',
        component: User
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

export default router
